package com.coding.util;

import com.coding.pojo.VideoCommentReply;
import com.coding.pojo.VideoCommentReplyRelate;
import com.coding.pojo.VideoComments;
import com.coding.pojo.Videos;
import com.coding.vo.VideoReplyData;
import com.coding.vo.VideoStorageData;
import org.springframework.stereotype.Component;

import java.util.*;

//视频评论数据初始化工具
@Component
public class CommentInitializer {

    //    初始化工具入口
    public List<VideoStorageData> initializer(List<Videos> videos, List<VideoComments> originData, List<VideoCommentReplyRelate> relates, List<VideoCommentReply> originReply) {

        Set<Long> commentSets = new HashSet<>();

//        获取所有评论和所有回复之间的关系集合
        for (VideoComments key : originData) {
            if (!commentSets.contains(key)) {
                commentSets.add(key.getId());
            }
        }

//        格式化回复数据，添加到对应评论中
        Map<Long, List<VideoCommentReply>> handledReply = handleReply(commentSets, originReply, relates);

//        格式化评论数据，添加到对应视频中
        List<VideoComments> comments = handleComments(originData, handledReply);

        return initCommentData(videos,comments);
    }

    /**
     * @param replies 回复列表
     * @param relates 回复关系列表
     * @return 处理好的回复信息
     */
    private Map<Long, List<VideoCommentReply>> handleReply(Set<Long> commentSets, List<VideoCommentReply> replies, List<VideoCommentReplyRelate> relates) {


        Map<Long, List<VideoCommentReply>> replyMap = new HashMap<>();
        List<VideoCommentReply> list = new ArrayList<>();

        for (VideoCommentReplyRelate relate : relates) {
//            清空列表
            list.clear();
            if (commentSets.contains(relate.getCommentId())) {
                for (VideoCommentReply reply : replies) {
                    if (reply.getId() == relate.getReplyId()) {
                        list.add(reply);
                    }
                }
                replyMap.put(relate.getCommentId(), list);
            }
        }
        return replyMap;
    }

//    将评论对应的回复添加到每个评论对象的列表中
    private List<VideoComments> handleComments(List<VideoComments> comments, Map<Long, List<VideoCommentReply>> replies) {
        for (VideoComments comment : comments){
            if (replies.containsKey(comment.getId())){
                comments.get(comments.indexOf(comment)).setReplyList(replies.get(comment.getId()));
            }
        }
        return comments;
    }

//    将所有评论放入对应的视频信息中，完成初始化
    private List<VideoStorageData> initCommentData(List<Videos> videos,List<VideoComments> comments){
        List<VideoStorageData> data = new ArrayList<>();
        for (Videos video : videos){
            List<VideoComments> commentsList = new ArrayList<>();
            VideoStorageData temp = new VideoStorageData();
            for (VideoComments comment : comments){
//                根据评论的视频id添加到对应的视频中
                if(comment.getVideoId() == video.getId()){
                    commentsList.add(comment);
                }
            }
//            赋值
            temp.setVideoId(video.getId());
            temp.setUrl(video.getUrl());
            temp.setTitle(video.getTitle());
            temp.setDescription(video.getDescription());
            temp.setCover(video.getCover());
            temp.setUserId(video.getUserId());
            temp.setComments(commentsList);
            temp.setLikes(video.getLikes());
            temp.setFavourites(video.getFavourites());
            temp.setShares(video.getShares());
            data.add(temp);
        }
//
//        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//        System.out.println(data);

        return data;
    }
}
